package servlet;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import pojo.User;
import service.UserManager;
import service.factory.UserManagerFactory;

 
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private UserManager manager;
 
    public RegistServlet() {
        super();
        manager = UserManagerFactory.getInstance();
    }

  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
	}

 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 String name = request.getParameter("name");
		 String psd = request.getParameter("password");

		 User user = new User(UUID.randomUUID().toString(), name, psd);
			
		 int m = manager.addUser(user);
		 
		  System.out.println("用户" + name + "注册请求");
		 if(m > 0){
			 Logger.getLogger(RegistServlet.class).info("用户 "+name+" 注册成功");
			 
			 request.getRequestDispatcher("login.jsp").forward(request, response);
		 } else {
			 Logger.getLogger(RegistServlet.class).info("用户 "+name+" 注册失败");
			 request.getRequestDispatcher("regist.jsp").forward(request, response);
		 }

	}

}
